package com.sm.sales_management.controller;


import com.sm.sales_management.domain.Admin;
import com.sm.sales_management.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/index")
    public String index(HttpSession session){
        Object admin=session.getAttribute("ok");
        if (admin!=null){
            return "index";
        }
        return "redirect:/admin/login";
    }

    @RequestMapping("/tologin")
    public String tologin(String username, String password, HttpSession session){
        Admin findAdmin=adminService.login(username);
        if (findAdmin.getPassword().equals(password)&&findAdmin.getUsername().equals(username)){
            session.setAttribute("ok",findAdmin);
            return "redirect:/admin/index";
        }
        return "redirect:/admin/login";

    }

    @RequestMapping("/toregister")
    public String toregister(){
        return "register";

    }
    @RequestMapping("/register")
    public String register(Admin admin){
        adminService.register(admin);
        return "redirect:/admin/login";

    }
}
